import datetime
import logging
import random
from rest_framework.response import Response
from rest_framework.views import APIView
from cars.models import Car
from persons.models import Person
from cars import tasks as cartask
from persons import tasks as persontask

logger = logging.getLogger("log")


# 人员进出回调
class VisitorCarCallBack(APIView):

    def post(self, request, format=None):
        logger.info("================人员进出回调==开始=================")
        uuid = request.data["uuid"]
        logger.info("================uuid==" + uuid + "=================")
        try:
            gate = request.data["gate"]
        except Exception:
            gate = ""
        logger.info("================gate==" + gate + "=================")
        status = request.data["status"]
        logger.info("================status==" + str(status) + "=================")
        kind = request.data["kind"]
        logger.info("================kind==" + str(kind) + "=================")
        if status == 1:
            try:
                person = Person.objects.get(uuid=uuid)
            except Person.DoesNotExist:
                logger.error("uuid=" + uuid + "不存在")
                rand = str(random.uniform(0, 9))
                ret = {"code": 1, "msg": "ok", "rand": rand}
                logger.info("================人员进出回调==结束=================")
                return Response(ret)
            if kind == 0:
                person.status = 3
                person.enter_time = datetime.datetime.now().strftime(
                    "%Y-%m-%d %H:%M:%S"
                )
                person.enter_gate_name = gate
                person.save()
            else:
                person.status = 4
                person.out_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                person.out_gate_name = gate
                person.save()
        # 教师邀请工单人员入场状态同步
        persontask.teacher_personenter_sync()
        # 教师邀请工单人员出场状态同步
        persontask.teacher_personout_sync()
        # 访客申请工单人员入场状态同步
        persontask.visitor_personenter_sync()
        # 访客申请工单人员出场状态同步
        persontask.visitor_personout_sync()
        # 学生邀请工单人员入场状态同步
        persontask.teacher_personenter_sync()
        # 学生邀请工单人员出场状态同步
        persontask.teacher_personout_sync()
        rand = str(random.uniform(0, 9))
        ret = {"code": 1, "msg": "ok", "rand": rand}
        logger.info("================人员进出回调==结束=================")
        return Response(ret)


# 车辆入场回调
class EnterCarCallBack(APIView):

    def post(self, request, format=None):
        logger.info("================车辆入场回调==开始=================")
        data = request.data["data"]
        enterTime = data["enterTime"]
        gateName = data["gateName"]
        carNo = data["carNo"]
        print("data", data)
        print("enterTime", enterTime)
        print("gateName", gateName)
        print("carNo", carNo)
        logger.info("车辆=={}".format(carNo))
        try:
            car = Car.objects.get(plate=carNo)
        except Car.DoesNotExist:
            logger.error("车辆=={}不存在".format(carNo))
            ret = {"code": 1, "msg": "ok", "rand": str(random.uniform(0, 9))}
            logger.info("================车辆入场回调==结束=================")
            return Response(ret)
        car.status = 3
        car.enter_time = enterTime
        car.enter_gate_name = gateName
        car.save()
        # 教师邀请工单车辆出场状态同步
        cartask.teacher_carenter_sync()
        # 访客申请工单车辆出场状态同步
        cartask.visitor_carenter_sync()
        ret = {"code": 1, "msg": "ok", "rand": str(random.uniform(0, 9))}
        logger.info("================车辆入场回调==结束=================")
        return Response(ret)


# 车辆出场回调
class OutCarCallBack(APIView):
    def post(self, request, format=None):
        logger.info("================车辆出场回调==开始=================")
        data = request.data["data"]
        outTime = data["outTime"]
        gateName = data["gateName"]
        carNo = data["carNo"]
        logger.info("车辆=={}".format(carNo))
        try:
            car = Car.objects.get(plate=carNo)
        except Car.DoesNotExist:
            logger.error("车辆=={}不存在".format(carNo))
        car.status = 4
        car.out_time = outTime
        car.out_gate_name = gateName
        car.save()
        # 教师邀请工单车辆出场状态同步
        cartask.teacher_carout_sync()
        # 访客申请工单车辆出场状态同步
        cartask.visitor_carout_sync()
        rand = str(random.uniform(0, 9))
        ret = {"code": 1, "msg": "ok", "rand": rand}
        logger.info("================车辆入场回调==结束=================")
        return Response(ret)
